const mongoose = require("mongoose");

const GroupMemberSchema = new mongoose.Schema(
  {
    group: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Group",
      required: true,
    },
    user: { type: mongoose.Schema.Types.ObjectId, ref: "User", required: true },
    role: {
      type: String,
      enum: ["owner", "admin", "member"],
      default: "member",
    },
    title: { type: String, trim: true, default: "" },
    positionId: { type: Number, default: null },
    status: { type: String, enum: ["pending", "accepted"], default: "pending" },
    requestType: {
      type: String,
      enum: ["invited", "applied"],
      default: "invited",
    }, // invited: 被邀请, applied: 主动申请
  },
  { timestamps: true }
);

GroupMemberSchema.index({ group: 1, user: 1 }, { unique: true });

const GroupMember = mongoose.model("GroupMember", GroupMemberSchema);

module.exports = GroupMember;
